package com.example.safe.keyboard;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.safe.keyboard.databinding.DialogSafekeyboardBinding;
import com.surfin.safe.keyboard.SafeKeyboard;

import java.util.Objects;

public class SafeKeyboardDialogFragment extends DialogFragment {

    private SafeKeyboard safeKeyboard;

    private DialogSafekeyboardBinding binding;

    // 键盘隐藏的时候是否关闭弹窗
    private boolean dismissWhenKeyboardHide = false;

    public static SafeKeyboardDialogFragment newInstance() {
        return new SafeKeyboardDialogFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, getTheme());
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new Dialog(Objects.requireNonNull(getContext()), getTheme()) {
            @Override
            public void dismiss() {
                if (safeKeyboard != null && safeKeyboard.stillNeedOptManually(false)) {
                    // 点击返回的时候，如果键盘在显示，先隐藏键盘
                    safeKeyboard.hideKeyboard();
                    if (dismissWhenKeyboardHide) {
                        super.dismiss();
                        dismissWhenKeyboardHide = false;
                    }
                    return;
                }
                super.dismiss();
            }
        };
    }

    @Override
    public void onStart() {
        super.onStart();
        // Dialog全屏显示
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            Window window = dialog.getWindow();
            if (window != null) {
                window.setLayout(width, height);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogSafekeyboardBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        safeKeyboard = new SafeKeyboard(view.getContext(), binding.llContainer, binding.ctlRoot);
        safeKeyboard.putEditText(binding.etText);
        super.onViewCreated(view, savedInstanceState);
        binding.tvSure.setOnClickListener(v -> {
            if (getDialog() != null) {
                dismissWhenKeyboardHide = true;
                getDialog().dismiss();
            }
        });
    }

    @Override
    public void onDestroyView() {
        if (safeKeyboard != null) {
            safeKeyboard.release();
            safeKeyboard = null;
        }
        super.onDestroyView();
    }
}
